package com.example.musicapp.ui.recommend

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.example.musicapp.R
import com.example.musicapp.databinding.ItemGridSongBinding
import com.example.musicapp.model.Song

class RecommendedSongsAdapter(
    private val songs: List<Song>,
    private val onSongClick: (Song) -> Unit
) : RecyclerView.Adapter<RecommendedSongsAdapter.ViewHolder>() {

    inner class ViewHolder(private val binding: ItemGridSongBinding) : 
        RecyclerView.ViewHolder(binding.root) {
        
        fun bind(song: Song) {
            binding.apply {
                songTitle.text = song.title
                artistName.text = song.artist
                
                Glide.with(songCover)
                    .load(song.coverUrl)
                    .placeholder(R.drawable.default_cover)
                    .transform(RoundedCorners(8))
                    .into(songCover)

                root.setOnClickListener { onSongClick(song) }
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemGridSongBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(songs[position])
    }

    override fun getItemCount() = songs.size
} 